package com.wcy.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.domain.Notice;
import com.wcy.model.R;
import com.wcy.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>Title:NoticeController.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/24 21:26</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@RestController
@Api(tags = "公告管理")
@RequestMapping("/notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping
    @ApiOperation("分页查询公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页大小"),
            @ApiImplicitParam(name = "title",value = "标题"),
            @ApiImplicitParam(name = "startTime",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",value = "结束时间"),
            @ApiImplicitParam(name = "status",value = "状态")
    })
    @PreAuthorize("hasAuthority('notice_query')")
    public R<Page<Notice>> findByPage(@ApiIgnore Page<Notice> page, String title, String startTime, String endTime, Integer status){
        page.addOrder(OrderItem.desc("last_update_time"));
        return R.ok(noticeService.findByPage(page,title,startTime,endTime,status));
    }

    @PostMapping("/delete")
    @ApiOperation("批量删除公告")
    @ApiImplicitParam(name = "ids",value = "id数组")
    @PreAuthorize("hasAuthority('notice_delete')")
    public R deleteIds(@RequestBody Long[] ids){
        boolean remove=noticeService.deleteIds(ids);
        if(remove){
            return R.ok();
        }
        return R.fail("删除失败");
    }

    @PostMapping("/updateStatus")
    @ApiOperation("启用/禁用 公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "公告id"),
            @ApiImplicitParam(name = "status",value = "要修改为的状态")
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R updateStatus(Long id,String status){
        Notice notice=new Notice();
        notice.setId(id);
        notice.setStatus(Integer.valueOf(status));
        boolean update = noticeService.updateById(notice);
        if(update){
            return R.ok();
        }
        return R.fail("状态修改失败");
    }

    @PostMapping
    @ApiOperation(value = "新增一个公告")
    @PreAuthorize("hasAuthority('notice_create')")
    public R add(@RequestBody @Validated Notice notice){
        notice.setStatus(1);
        boolean save = noticeService.save(notice);
        if(save){
            return R.ok();
        }
        return R.fail("新增失败");
    }

    @PatchMapping
    @ApiOperation(value = "修改一个公告")
    @PreAuthorize("hasAuthority('notice_update')")
    public R update(@RequestBody Notice notice){
        boolean update = noticeService.updateById(notice);
        if(update){
            return R.ok();
        }
        return R.fail("修改失败");
    }

}
